package com.unitedvision.persistence.domain;

import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import com.unitedvision.core.domain.Module;

@Entity
@Table (name = "module")
public class ModuleEntity {
	private int id;
	private String name;
	private long price;
	private List<CompanyModuleEntity> companyModules;
	
	public ModuleEntity() {
		super();
	}

	public ModuleEntity(Module module) {
		super();
		this.id = module.getId();
		this.name = module.getName();
		this.price = module.getPrice();
	}

	public ModuleEntity(String name, long price) {
		super();
		this.name = name;
		this.price = price;
	}

	@Id @GeneratedValue
	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	@Column (name = "name")
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Column (name = "price")
	public long getPrice() {
		return price;
	}

	public void setPrice(long price) {
		this.price = price;
	}
	
	@OneToMany (targetEntity = CompanyModuleEntity.class, mappedBy = "module", fetch = FetchType.LAZY)
	public List<CompanyModuleEntity> getCompanyModules() {
		return companyModules;
	}

	public void setCompanyModules(List<CompanyModuleEntity> companyModules) {
		this.companyModules = companyModules;
	}

	public Module toModule() {
		return new Module(id, name, price);
	}
	
	public static ModuleEntity create(String name, long price) {
		return new ModuleEntity(name, price);
	}
	
	public static ModuleEntity create(Module module) {
		return new ModuleEntity(module);
	}

	@Override
	public String toString() {
		return "ModuleEntity [id=" + id + ", name=" + name + ", price=" + price
				+ "]";
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + id;
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		result = prime * result + (int) (price ^ (price >>> 32));
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		ModuleEntity other = (ModuleEntity) obj;
		if (id != other.id)
			return false;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		if (price != other.price)
			return false;
		return true;
	}
}
